﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KeywordAuction;

namespace Simulation.Strategy
{
    public abstract class MultipleValueStrategyBase : MultipleStrategyBase
    {
        public double Value { get; set; }
    }

    public abstract class MultipleStrategyBase : StrategyBase
    {
        public abstract bool Action(Advertiser advertiser,int index);
    }
    public class MultipleStrategyContainerStrategy : StrategyBase
    {
        public MultipleStrategyContainerStrategy():base()
        {
            StrategyList = new List<MultipleStrategyBase>();
        }
        public List<MultipleStrategyBase> StrategyList{ get; set; }
        public override bool Action(Advertiser advertiser)
        {
            int index = 0;
            foreach (var item in StrategyList)
            {
                bool result = item.Action(advertiser,index);
                if (result != true)
                {
                    return false;
                }
                index++;
            }
            if (advertiser.CurrentUsingBudget > advertiser.BudgetConstraint)
            {
                return false;
            }
            base.Action(advertiser);
            return true;
        }

    }

    public class MultipleStrategyRaise : MultipleStrategyBase
    {
        public override bool Action(Advertiser advertiser, int index)
        {
            var item = advertiser.Biddings[index];

            int AadIndex = item.AttendingAdspot.GetIndexAdvertiser(item);
            if (AadIndex != 0)
            {
                var value = item.AttendingAdspot.Bitting.OrderByDescending(p => p.Bid).ToList()[AadIndex - 1].Bid;
                item.Bid = value + 1;

            }
            else
            {
                return false;
            }
            base.Action(advertiser);
            return true;
        }
    }
    public class MultipleStrategyDrop : MultipleStrategyBase
    {
        public override bool Action(Advertiser advertiser, int index)
        {
            var item = advertiser.Biddings[index];

            int AadIndex = item.AttendingAdspot.GetIndexAdvertiser(item);
            if (AadIndex != item.AttendingAdspot.Bitting.Count - 1)
            {
                var value = item.AttendingAdspot.Bitting.OrderByDescending(p => p.Bid).ToList()[index + 1].Bid;

                if (item.Bid > 0)
                {
                    item.Bid = value - 1;
                }
                else
                {
                    return false;
                }
            }

            base.Action(advertiser);
            return true;
        }
    }
    public class MultipleStrategyHold : MultipleStrategyBase
    {
        public override bool Action(Advertiser advertiser,int index)
        {
            return base.Action(advertiser);
        }
    }
    public class MultipleStrategyJump : MultipleStrategyBase
    {
        public override bool Action(Advertiser advertiser, int index)
        {
            var item = advertiser.Biddings[index];
            item.Bid = item.ExpectedRevenue - 1;
            return base.Action(advertiser);
        }
    }


    public class MultipleStrategyRaiseValue : MultipleValueStrategyBase
    {
        public override bool Action(Advertiser advertiser, int index)
        {
            var item = advertiser.Biddings[index];
            item.Bid += Value;
            return base.Action(advertiser);
        }
    }

    public class MultipleStrategyDropValue : MultipleValueStrategyBase
    {
        public override bool Action(Advertiser advertiser, int index)
        {
            var item = advertiser.Biddings[index];
            if (item.Bid - Value < 0)
            {
                return false;
            }
            item.Bid -= Value;
            return base.Action(advertiser);
        }
    }
}
